package longrise.phone.com.bjjt_jyb.View;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.PopupWindow;

import longrise.phone.com.bjjt_jyb.View.AbstractSpinerAdapter.IOnItemSelectListener;
import longrise.phone.com.bjjt_jyb.R;

import java.util.List;

public class SpinerPopWindow2 extends PopupWindow implements OnItemClickListener{

	private Context mContext;
	private ListView mListView;
	private NormalSpinerAdapter2 mAdapter;
	private IOnItemSelectListener mItemSelectListener;
	private View mView;
	private boolean isWarpContent = false; 
	
	private double multiple = 0 ; //固定倍数
	
	public void setMultiple(double multiple) {
		this.multiple = multiple;
	}


	public void setWarpContent(boolean isWarpContent) {
		this.isWarpContent = isWarpContent;
		this.init();
	}


	public SpinerPopWindow2(Context context)
	{
		super(context);		
		mContext = context;
		init();
	}
	
	public void setItemListener(IOnItemSelectListener listener){
		mItemSelectListener = listener;
	}

	
	@SuppressWarnings("deprecation")
	private void init()
	{
		View view = LayoutInflater.from(mContext).inflate(R.layout.spiner_window_layout2, null);
		setContentView(view);		
		setWidth(LayoutParams.WRAP_CONTENT);
		setHeight(LayoutParams.WRAP_CONTENT);
		
		setFocusable(true);
    	ColorDrawable dw = new ColorDrawable(0x00);
		setBackgroundDrawable(dw);
	
		
		mListView = (ListView) view.findViewById(R.id.listview);
		if(!isWarpContent) {
			//设置listview的高度为屏幕的半个高度
			WindowManager window = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
			int height = window.getDefaultDisplay().getHeight();
			if(multiple == 0) {
				multiple = 0.5;
			}
			mListView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, (int)(height*multiple)));
		}else{
			//设置listview的高度为warpcontent
			mListView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
		}	
		mAdapter = new NormalSpinerAdapter2(mContext);
		mListView.setAdapter(mAdapter);	
		mListView.setOnItemClickListener(this);

	}
	
	
	public void refreshData(List<String> list, int selIndex)
	{
		if (list != null && selIndex  != -1)
		{
			mAdapter.refreshData(list, selIndex);
		}
	}
	
	public void setView(View view) {
		this.mView = view;
	}


	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int pos, long arg3) {
		dismiss();
		if (mItemSelectListener != null){
			mItemSelectListener.onItemClick(pos, mView);
		}
	}


	
}
